package com.hua.api.core.framework.sms;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SmsSenderBak {

    private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
    private static String APIKEY="32977319565429615312aa2f4e69384c";
    private static String APIID="C40637644";
//    private static String APIKEY="30c77b328752df61cbee128384c4059f";
//    private static String APIID="C00589539";


    public static boolean sendSms(String phoneNumber,String content){
        
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(Url);

        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        Long timel = System.currentTimeMillis()/1000;
        String timestr = timel.toString();
        String src = APIID+APIKEY+phoneNumber+content+timestr;

        SecureUtil.md5(src);

        String password =  SecureUtil.md5(src); // MD5Util.smsEncrypt(src);

        NameValuePair[] data = {
                new NameValuePair("account", APIID),
                new NameValuePair("password", password),
                new NameValuePair("mobile", phoneNumber),
                new NameValuePair("content", content),
                new NameValuePair("time", timestr),
                new NameValuePair("format", "json"),
        };

        method.setRequestBody(data);

        try {
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            JSONObject resultJson = JSONObject.parseObject(result);
            Integer code =resultJson.getInteger("code");
            if(2==code){
                return true;
            }
            // {"code":2,"msg":"\u63d0\u4ea4\u6210\u529f","smsid":"15257469376056454000"}
            //logger.logInfo(result);
            //System.out.println(result);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return false;
    }
}
